<!--
 * @Date: 2022-08-25 14:05:59
 * @LastEditors: StavinLi 495727881@qq.com
 * @LastEditTime: 2023-05-24 15:20:53
 * @FilePath: /Workflow-Vue3/src/components/drawer/copyerDrawer.vue
-->
<template>
  <el-drawer
    :append-to-body="true"
    title="抄送人设置"
    v-model="visible"
    class="set_copyer"
    :show-close="false"
    :size="550"
    :before-close="saveCopyer"
  >
    <div class="demo-drawer__content">
      <div class="copyer_content drawer_content">
        <el-button type="primary" @click="addCopyer">添加成员</el-button>
        <p class="selected_list">
          <span v-for="(item, index) in copyerConfig.nodeUserList" :key="index"
            >{{ item.name }}
            <img
              src="../images/add-close1.png"
              @click="$func.removeEle(copyerConfig.nodeUserList, item, 'targetId')"
            />
          </span>
          <a
            v-if="copyerConfig.nodeUserList && copyerConfig.nodeUserList.length != 0"
            @click="copyerConfig.nodeUserList = []"
            >清除</a
          >
        </p>
        <el-checkbox-group v-model="ccSelfSelectFlag" class="clear">
          <el-checkbox :value="1">允许发起人自选抄送人</el-checkbox>
        </el-checkbox-group>
      </div>
      <div class="demo-drawer__footer clear">
        <el-button type="primary" @click="saveCopyer">确 定</el-button>
        <el-button @click="closeDrawer">取 消</el-button>
      </div>
      <employees-role-dialog
        v-model:visible="copyerVisible"
        :data="checkedList"
        @change="sureCopyer"
      />
    </div>
  </el-drawer>
</template>
<script setup>
import employeesRoleDialog from '../dialog/employeesRoleDialog.vue'
import $func from '../utils/index'
import { ref, watch, computed, inject } from 'vue'

const props = defineProps({
  internalState: {
    type: Object,
    default: null,
  },
})

const copyerConfig = ref({})
const ccSelfSelectFlag = ref([])
const copyerVisible = ref(false)
const checkedList = ref([])

// 获取内部状态管理或使用全局store作为fallback
const flowDesignerState = inject('flowDesignerState', null)
// 使用内部状态管理
const state = flowDesignerState.state
const actions = flowDesignerState.actions

const copyerDrawer = computed(() => state.copyerDrawer)
const copyerConfig1 = computed(() => state.copyerConfig1)
const visible = computed({
  get() {
    return copyerDrawer.value
  },
  set() {
    closeDrawer()
  },
})
watch(copyerConfig1, (val) => {
  copyerConfig.value = val.value
  ccSelfSelectFlag.value =
    copyerConfig.value.ccSelfSelectFlag == 0 ? [] : [copyerConfig.value.ccSelfSelectFlag]
})

const addCopyer = () => {
  copyerVisible.value = true
  checkedList.value = copyerConfig.value.nodeUserList
}
const sureCopyer = (data) => {
  copyerConfig.value.nodeUserList = data
  copyerVisible.value = false
}
const saveCopyer = () => {
  copyerConfig.value.ccSelfSelectFlag = ccSelfSelectFlag.value.length == 0 ? 0 : 1
  copyerConfig.value.error = !$func.copyerStr(copyerConfig.value)
  actions.setCopyerConfig({
    value: copyerConfig.value,
    flag: true,
    id: copyerConfig1.value.id,
  })
  closeDrawer()
}
const closeDrawer = () => {
  actions.setCopyer(false)
}
</script>

<style lang="less">
.set_copyer {
  .copyer_content {
    padding: 20px 20px 0;

    .el-button {
      margin-bottom: 20px;
    }

    .el-checkbox {
      margin-bottom: 20px;
    }
  }
}
</style>
